package com.appkubes.mall.restful.application.express.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.entity.MallKd;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.express.model.KdCallbackModel;
import com.appkubes.mall.restful.application.express.model.KdResult;
import com.appkubes.mall.restful.application.express.service.impl.KdServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "快递模块")
@Slf4j
@RestController
@RequestMapping("/api/Ckd")
public class KdController extends BaseController {

    @Autowired
    KdServiceImpl kdService;

    @ApiOperation(value = "获取订单物流信息")
    @PostMapping("/getKdTraces")
    public Response getKdTraces(String orderNo) {
        KdResult kd = kdService.queryKdTrace(orderNo);
        if (kd == null) {
            return Response.error("订单号:" + orderNo + ",无物流信息");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("traces", JSON.parse(kd.getTraces()));
        map.put("company_name", kd.getName());
        map.put("order_no", kd.getLogisticCode());
        map.put("state", kd.getState());
        map.put("delivery_time", kd.getEstimatedDeliveryTime());
        return Response.success(map);
    }

    @RequestMapping(value = "callback", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> callback(String RequestData, String RequestType, String DataSign) {
        RequestData = StringEscapeUtils.unescapeHtml4(RequestData);

        JSONObject jsonObject = JSONObject.parseObject(RequestData);
        KdCallbackModel callbackModel = new KdCallbackModel();
        callbackModel.setPushTime(jsonObject.getDate("PushTime"));
        callbackModel.setCount(jsonObject.getString("Count"));
        callbackModel.setData(jsonObject.getString("Data"));
        callbackModel.setEBusinessID(jsonObject.getString("EBusinessID"));

        if (callbackModel.getData().equals("[]")) {
            // nothing to do
        } else {
            JSONArray jsonArray = jsonObject.getJSONArray("Data");
            JSONObject jsonObj = JSONObject.parseObject(jsonArray.getString(0));

            MallKd kd = new MallKd();
            kd.setCount(Integer.valueOf(callbackModel.getCount()));
            kd.setCreateTime(DateUtils.DateToTimestamp(new Date()));
            kd.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
            kd.setEBusinessID(callbackModel.getEBusinessID());
            kd.setEstimatedDeliveryTime(callbackModel.getPushTime());
            kd.setPushTime(callbackModel.getPushTime());
            kd.setLogisticCode(jsonObj.getString("LogisticCode"));
            kd.setShipperCode(jsonObj.getString("ShipperCode"));
            kd.setState(Integer.parseInt(jsonObj.getByte("State").toString()));
            if (jsonObj.getString("Traces") == null) {
                kd.setTraces("[]");
            } else {
                kd.setTraces(jsonObj.getString("Traces"));
            }
            kdService.saveOrUpdate(kd);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("EBusinessID", callbackModel.getEBusinessID());
        map.put("UpdateTime", DateUtils.DateToTimestamp(new Date()));
        map.put("Success", true);
        map.put("Reason", "");
        return map;
    }
}
